import { findAllCategory } from "@/apis/category"
import { topCategory } from "@/apis/constants"

export default {
    namespaced: true,
    state() {
        return {
            list: topCategory.map(name => ({ name }))
        }
    },
    mutations: {
        setList(state, payload) {
            state.list = payload
        },
        show(state, item) {
            const target = state.list.find(x => x.id === item.id)
            target.open = true
        },
        hide(state, item) {
            const target = state.list.find(x => x.id === item.id)
            target.open = false
        },
    },
    actions: {
        async findAllCategory(store) {
            try {
                let { result } = await findAllCategory()
                result = result.map(item => ({ ...item, open: false }))
                store.commit('setList', result)
            } catch (err) {
                console.log(err)
            }
        }
    }
}